clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"
/* Estimates using current location */
/********************/
/* Wages unbalanced */
/********************/
capture program drop wagescs
program define wagescs
version 11
syntax, occp(name)

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
keep if `occp'==1

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* if ptreat | dtreat
predict rlsnd, residuals

save temp1.dta, replace
collapse (mean) prlsnd=rlsnd if ptreat, by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse (mean) drlsnd=rlsnd if dtreat, by(ze an)
save p2.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge
joinby ze an using p2.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen rlsnd=drlsnd-L.prlsnd
/* OLS */
ivreg2 rlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 rlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv
end

wagescs, occp(all) 
wagescs, occp(cad) 
wagescs, occp(tecemp2) 
wagescs, occp(bcq) 
wagescs, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Tab21\Table7.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcqols bcnqols ///
using "C:\Users\Public\Documents\Tab21\Table7.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bcqiv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\Table7.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)

/*********************/
/* WAGES WAGES WAGES */
/*********************/
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\final.dta", clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

end

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 
/* Estimates using the baseline location */
/* OLS */
esttab bsallols  bscadols bstecemp2ols bsbcols bsbcnqols ///
using "C:\Users\Public\Documents\Tab21\Table7.rtf" /// 
, append title("bsOLS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
/* 2SLS */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\Table7.rtf" /// 
, append title("bsIV")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
